
import random
import datetime
money=int(input("请输入你目前携带的金额："))
goods_list=[["水杯",25,20],["酸奶",10,15],["香蕉",5,10],["面包",3,30],["空调",1328,5],["手机",2100,10]]    #商品列表(商品名称，商品价格,库存）
shopping_car=[]  #购物车列表
discount_list=[["空调八折券","空调",0.8],["水杯九折券","水杯",0.9],["酸奶买二送一","酸奶",1/3],["空调九折券","空调",0.9],["面包9折券","面包",0.9],
               ["手机九折券", "手机", 0.9], ["香蕉9折券", "香蕉", 0.9],["手机八折券", "手机", 0.8], ["香蕉买1送1", "香蕉", 0.5],["全场8折券", "全场", 0.8]]
#抽优惠券
def draw_coupons():
    global discount
    discount= random.choice(discount_list)
    print(f"恭喜您，抽中了优惠券：{discount[0]}，适用商品为：{discount[1]}")
    return discount
#打印购物小条
def print_receipt(coupon):
    print("\n=============== 购物小票 ===============")
    print(f"购物时间：{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"卡内余额：{money} 元")
    total_original = 0  # 商品原价总和
    total_discounted = 0  # 优惠后总价

    for name,price,kucun in goods_list:
        for name_shoping,price_shopping,num_shopping in shopping_car:
            if name==name_shoping:
                total_original += price*num_shopping
                total_discounted += total_original*discount[2]

                # 打印商品明细（含优惠说明）
                print(f"  商品名称：{name}  原价：{price} 元/件  数量：{num_shopping}  小计：{price*num_shopping} 元")
                if(name_shoping==coupon[1] or coupon[1]=='全场'):
                    print("  优惠：{:.2f}  最终价：{:.2f} 元".format(price-price*num_shopping*discount[2],price*num_shopping*discount[2]))
                else:
                    print("  优惠：{:.2f}  最终价：{:.2f} 元".format(0,price*num_shopping))
#开始购物
def shopping(coupon):
    print("\n=============== 欢迎进入商场 ===============")
    print("商品列表：")
    index = 0
    while index < len(goods_list):
        print(f"{index + 1}.  商品：{goods_list[index][0]}  价格：{goods_list[index][1]}  库存：{goods_list[index][2]}")
        index += 1
    while True:
        choice = input("\n请输入商品序号选购（输入 q或Q 退出选购，输入 r或R 退货）：")
        if choice == 'q' or choice == 'Q':
            break
        elif choice == "r" or choice=="R":
            return_goods()
        elif int(choice)>=1 and int(choice)<=len(goods_list):
            seq = int(choice) - 1
            goods_name, goods_price,goods_kucun = goods_list[seq][0],goods_list[seq][1],goods_list[seq][2]
            #检查是否有优惠券
            if goods_name == coupon[1]:
                goods_price *= coupon[2]
                print(f"使用优惠券，{goods_name} 优惠后价格：{goods_price:.2f} 元")
            global money
            buy_num=int(input("请输入购买数量："))
            if buy_num<=0:
                print("购买数量不能小于等于 0，请重新输入！")
                continue
            if buy_num > goods_kucun:
                print(f"库存不足，该商品最多可购买 {goods_kucun} 件，请重新输入！")
                continue
            total_cost=buy_num*goods_price
            if money < total_cost:
                print("穷鬼，钱不够！请到其他超市购买！")
                continue
            #扣钱
            money -= total_cost
            #减少商品库存
            goods_list[seq][2]-=buy_num
            #添加到购物车
            shopping_car.append([goods_name,goods_price,buy_num])
            print(f"已加入购物车，当前余额：{money:.2f} 元")
            continue
        else:
            print("没有这个商品，别瞎弄！")

#退货
def return_goods():
    print("\n=== 退货功能 ===")
    if not shopping_car:
        print("购物车为空，无法退货")
        return
    print("购物车商品：")
    for index, goods in enumerate(shopping_car):
        print(f"{index + 1}. {goods[0]} 价格：{goods[1]} 元 数量：{goods[2]}")
    return_choice = input("请输入要退货的商品序号（输入 0 取消退货）：")
    if return_choice == "0":
        return
    elif int(return_choice)>=1 and int(return_choice)<= len(shopping_car):
        index = int(return_choice) - 1
        r_goods= shopping_car.pop(index)#退货商品
        global money
        money += r_goods[1]*r_goods[2]
        # 将商品放回商品列表
        for goods in goods_list:
            if goods[0] == r_goods[0]:
                goods[2] += r_goods[2]
                break
        print(f"{r_goods[0]} 已退货，余额恢复：{money:.2f} 元")
    else:
        print("输入无效，取消退货")
# 抽优惠券
coupon = draw_coupons()
# 开始购物
shopping(coupon)
# 打印购物小条
if shopping_car:
    print_receipt(coupon)
else:
    print("未购买商品，购物结束")